﻿using System;
using System.Collections.Generic;
using ESVBible.Service.Parameter;
using ESVBible.Service.Setting;
using General.Net;
//+
namespace ESVBible.Service
{
    public class RESTServiceCaller
    {
        //- $ArgumentList -//
        private Dictionary<String, String> ArgumentList { get; set; }

        //+
        //- @Ctor -//
        public RESTServiceCaller()
        {
            this.ArgumentList = new Dictionary<String, String>();
        }

        //+
        //- ~CallDailyVerseMethod -//
        internal String CallDailyVerseMethod(DailyVerseParameter parameters, IOutputSetting settings)
        {
            ArgumentListManager.InitArgumentList(this.ArgumentList, parameters.Key);
            ArgumentListManager.ApplyArguments(this.ArgumentList, parameters);
            //+
            return this.CallMethod(RESTEndpoint.DailyVerse, settings);
        }

        //- $CallMethod -//
        private String CallMethod(String endpoint, IOutputSetting settings)
        {
            ArgumentListManager.ApplyArguments(this.ArgumentList, settings);
            String argumentList = ArgumentListManager.GetArgumentString(this.ArgumentList);
            //+
            return HttpAbstractor.GetWebText(this.GetAbsoluteUri(endpoint, argumentList), settings.Timeout);
        }

        //- $GetAbsoluteUri -//
        private Uri GetAbsoluteUri(String endpoint, String argumentList)
        {
            return new Uri(String.Format("{0}?{1}", endpoint, argumentList));
        }

        //- ~CallPassageQueryMethod -//
        internal String CallPassageQueryMethod(PassageQueryParameter parameters, IOutputSetting settings)
        {
            ArgumentListManager.InitArgumentList(this.ArgumentList, parameters.Key);
            ArgumentListManager.AddArgument(this.ArgumentList, "passage", parameters.Passage);
            //+
            return this.CallMethod(RESTEndpoint.PassageQuery, settings);
        }

        //- ~CallQueryMethod -//
        internal String CallQueryMethod(QueryParameter parameters, IOutputSetting settings)
        {
            ArgumentListManager.InitArgumentList(this.ArgumentList, parameters.Key);
            ArgumentListManager.ApplyArguments(this.ArgumentList, parameters);
            //+
            return this.CallMethod(RESTEndpoint.Query, settings);
        }

        //- ~CallQueryInfoMethod -//
        internal String CallQueryInfoMethod(QueryInfoParameter parameters, IOutputSetting settings)
        {
            ArgumentListManager.InitArgumentList(this.ArgumentList, parameters.Key);
            ArgumentListManager.AddArgument(this.ArgumentList, "q", parameters.Q);
            //+
            return this.CallMethod(RESTEndpoint.QueryInfo, settings);
        }

        //- ~CallReadingPlanInfoMethod -//
        internal String CallReadingPlanInfoMethod(ReadingPlanInfoParameter parameters, IOutputSetting settings)
        {
            ArgumentListManager.InitArgumentList(this.ArgumentList, parameters.Key);
            ArgumentListManager.ApplyArguments(this.ArgumentList, parameters);
            //+
            return this.CallMethod(RESTEndpoint.ReadingPlanInfo, settings);
        }

        //- ~CallReadingPlanQueryMethod -//
        internal String CallReadingPlanQueryMethod(ReadingPlanQueryParameter parameters, IOutputSetting settings)
        {
            ArgumentListManager.InitArgumentList(this.ArgumentList, parameters.Key);
            ArgumentListManager.ApplyArguments(this.ArgumentList, parameters);
            //+
            return this.CallMethod(RESTEndpoint.ReadingPlanQuery, settings);
        }

        //- ~CallVerseMethod -//
        internal String CallVerseMethod(VerseParameter parameters, IOutputSetting settings)
        {
            ArgumentListManager.InitArgumentList(this.ArgumentList, parameters.Key);
            ArgumentListManager.ApplyArguments(this.ArgumentList, parameters);
            //+
            return this.CallMethod(RESTEndpoint.Verse, settings);
        }
    }
}